package com.lsy.weizhi2.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.lsy.weizhi2.utils.RegexUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Forget_Password extends MyBaseActivity implements View.OnClickListener,View.OnFocusChangeListener {
    private View forget, back_view,register_invite_line,forget_phone_line,forget_code_line,forget_password_line;
    private EditText forget_phone, forget_password, forget_invite, forget_code;
    private MyTitleView forget_title;
    private TextView forget_code_tv;
    private ImageView forget_show_password_iv, forget_invite_iv,forget_phone_iv,forget_password_iv,forget_code_iv;
    private Button forget_btn;
    private Context context;
    private boolean ispwshow;
    private int xx, xxx,height;
    private Handler handler = new Handler();
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long l) {
            forget_code_tv.setText(l / 1000 + "秒后可重发");
        }

        @Override
        public void onFinish() {
            forget_code_tv.setClickable(true);
            forget_code_tv.setHint("获取验证码");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget__password);
        Init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ViewTreeObserver vto = forget_title.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xx < 1) {
                    xx++;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(forget_title.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                forget_title.setLayoutParams(params);
                            }

                        }
                    });
                }
            }
        });
        ViewTreeObserver vto2= back_view.getViewTreeObserver();
        vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (xxx < 1) {
                    xxx++;
                    height=back_view.getHeight();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(back_view.getLayoutParams());
                            params.height = MyHeight.ACTIONBAR + height;
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                back_view.setLayoutParams(params);
                            }

                        }
                    });
                }
            }
        });
    }

    private void Init() {
        context = Forget_Password.this;
        back_view = findViewById(R.id.back_view);
        forget = findViewById(R.id.forget);
        forget_title= (MyTitleView) findViewById(R.id.forget_title);
        forget_phone = (EditText) forget.findViewById(R.id.register_phone_et);
        forget_code = (EditText) forget.findViewById(R.id.register_code_et);
        forget_password = (EditText) forget.findViewById(R.id.register_password_et);
        forget_invite = (EditText) forget.findViewById(R.id.register_invite_et);
        forget_code_tv = (TextView) forget.findViewById(R.id.register_code_tv);
        forget_show_password_iv = (ImageView) forget.findViewById(R.id.register_show_password_iv);
        forget_invite_iv = (ImageView) forget.findViewById(R.id.register_invite_iv);
        forget_phone_iv = (ImageView) forget.findViewById(R.id.register_phone_iv);
        forget_password_iv = (ImageView) forget.findViewById(R.id.register_password_iv);
        forget_code_iv = (ImageView) forget.findViewById(R.id.register_code_iv);
        forget_btn = (Button) forget.findViewById(R.id.register_btn);
        register_invite_line=findViewById(R.id.register_invite_line);
        forget_phone_line=findViewById(R.id.register_phone_line);
        forget_code_line=findViewById(R.id.register_code_line);
        forget_password_line=findViewById(R.id.register_password_line);
        forget_invite.setVisibility(View.GONE);
        forget_invite_iv.setVisibility(View.GONE);
        register_invite_line.setVisibility(View.GONE);
        forget_btn.setText("提交");
        forget_code_tv.setOnClickListener(this);
        forget_show_password_iv.setOnClickListener(this);
        forget_btn.setOnClickListener(this);
        forget_phone.setOnFocusChangeListener(this);
        forget_code.setOnFocusChangeListener(this);
        forget_password.setOnFocusChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        String user_phone = forget_phone.getText().toString();
        switch (view.getId()) {
            case R.id.register_show_password_iv:
                if (ispwshow) {
                    forget_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    forget_show_password_iv.setImageResource(R.mipmap.login_ico1_10);
                    ispwshow=false;
                } else {
                    forget_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    forget_show_password_iv.setImageResource(R.mipmap.login_ico2_10);
                    ispwshow=true;
                }
                break;
            case R.id.register_code_tv:
                if (RegexUtils.checkMobile(user_phone)) {
                    Getcode(user_phone);
                } else {
                    MyMethod.ShowDialog(context,"请输入正确手机号码");
                }
                break;
            case R.id.register_btn:
               SendData(user_phone);
                break;
        }
    }

    private void Getcode(String phone) {
        MyOkHttpUtils.getInstance(context).doPostSMS(Constants.SEND_SMS + phone, null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {
                        Log.i("Forget_Password","message=fff="+message);
                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("Forget_Password","message=sss="+message);
                        if (message != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if (result) {
                                    timer.start();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }

    private void AllClearFocus() {
        forget_phone_iv.setImageResource(R.mipmap.login_ico2_03);
        forget_code_iv.setImageResource(R.mipmap.login_ico1_06);
        forget_password_iv.setImageResource(R.mipmap.login_ico2_08);
        forget_phone_line.setBackgroundResource(R.color.clicked_no);
        forget_code_line.setBackgroundResource(R.color.clicked_no);
        forget_password_line.setBackgroundResource(R.color.clicked_no);
        register_invite_line.setBackgroundResource(R.color.clicked_no);
    }

    private void PhoneHasFocus() {
        forget_phone_iv.setImageResource(R.mipmap.login_ico1_03);
        forget_phone_line.setBackgroundResource(R.color.clicked_ok);
    }

    private void CodeHasFocus() {
        forget_code_iv.setImageResource(R.mipmap.login_ico2_06);
        forget_code_line.setBackgroundResource(R.color.clicked_ok);
    }

    private void PWHasFocus() {
        forget_password_iv.setImageResource(R.mipmap.login_ico1_08);
        forget_password_line.setBackgroundResource(R.color.clicked_ok);
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {
            switch (view.getId()) {
                case R.id.register_phone_et:
                    AllClearFocus();
                    PhoneHasFocus();
                    break;
                case R.id.register_code_et:
                    AllClearFocus();
                    CodeHasFocus();
                    break;
                case R.id.register_password_et:
                    AllClearFocus();
                    PWHasFocus();
                    break;
            }
        }
    }
    //发送请求
    private void SendData(String phone){
        String password = forget_password.getText().toString();
        String code = forget_code.getText().toString();
        Map<String,String> map=new HashMap<>();
        if(password==null||password.length()<6){
            MyMethod.ShowDialog(context,"请输入6位以上密码");
            return;
        }else{
            map.put("newPassword",password);
        }
        map.put("username",phone);
        map.put("verifyCode",code);
        MyOkHttpUtils.getInstance(context).doPost(Constants.USER_FIND_PASSWORD,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {
                        Log.i("Forget_Password","message=fff="+message);
                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("Forget_Password","message=sss="+message);
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if(result){
                                    finish();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }
}
